package com.kyzh.core.pager.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gushenge.core.beans.GroupChatItem
import com.gushenge.core.requests.GameRequest
import com.kyzh.core.R
import com.kyzh.core.adapters.v3.HomeGroupChatBottomAdapter
import com.kyzh.core.adapters.v3.HomeGroupChatListAdapter
import kotlinx.android.synthetic.main.fragment_homegroupchat.*


class HomeGroupChatFragment : Fragment() {

    private val tuijians = arrayListOf<GroupChatItem>()
    private val tujianAdapter = HomeGroupChatListAdapter(R.layout.item_groupchat,tuijians)
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_homegroupchat, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        rvTuijian.adapter = tujianAdapter

        GameRequest.groupChat(0,1){p,max,list,tuijian->
            tuijians.clear()
            tujianAdapter.addData(tuijian)
            GameRequest.sort {
                val adapter = HomeGroupChatBottomAdapter(R.layout.recyclerview, it)
                viewPager.adapter = adapter
                viewPager.currentItem = 0
                val mediator = TabLayoutMediator(tabLayout, viewPager){ tab, position->
                    tab.text = it[position].name
                }
                mediator.attach()

                val textView: TextView = View.inflate(requireContext(), R.layout.tab_text, null) as TextView
                textView.text = tabLayout.getTabAt(0)?.text
                tabLayout.getTabAt(0)?.customView = textView
                tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                    override fun onTabSelected(tab: TabLayout.Tab?) {
                        val textView1: TextView = View.inflate(requireContext(), R.layout.tab_text, null) as TextView
                        textView1.text = tab?.text
                        tab?.customView = textView1
                    }
                    override fun onTabUnselected(tab: TabLayout.Tab?) {
                        tab?.customView = null
                    }
                    override fun onTabReselected(tab: TabLayout.Tab?) {
                    }
                })
            }

        }
    }

    override fun onResume() {
        super.onResume()

    }

}